using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PlugNT.App.Payment.Model;

namespace PlugNT.App.Payment.Web
{
    public partial class ReturnPage_PayPal : System.Web.UI.Page
    {

        protected global::System.Web.UI.HtmlControls.HtmlInputControl creditCardNumber;
        protected global::System.Web.UI.HtmlControls.HtmlInputControl amount;
        protected global::System.Web.UI.WebControls.DropDownList creditCardType;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {

                creditCardNumber.Value = GenerateCreditCardNumber(creditCardType.SelectedItem.Value);
                if (!string.IsNullOrEmpty(Request.QueryString["number"]))
                {
                    ViewState["number"] = Request.QueryString["number"];
                }
                if (!string.IsNullOrEmpty(Request.QueryString["number"]))
                {
                    ViewState["price"] = Request.QueryString["price"];
                    amount.Value = Request.QueryString["price"];
                }
                
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {


            if (ViewState["number"] != null)
            {
                string orderid = Convert.ToString(ViewState["number"]);
                string price = Convert.ToString(ViewState["price"]);
                try
                {
                    decimal dPrice = 0;  
                    if (dPrice < Convert.ToDecimal(price))
                    {

                        return;
                    }
                }
                catch
                {

                    return;
                }





























                if (1 == 1)
                {


                    Response.Redirect("../../orderdetail.aspx?id=" + orderid);
                }














































            }

        }

        public string GenerateCreditCardNumber(string cardType)
        {
            int[] cc_number = new int[16];
            int cc_len = 16;
            int start = 0;
            Random r = new Random();
            switch (cardType)
            {
                case "Visa":
                    cc_number[start++] = 4;
                    break;
                case "Discover":
                    cc_number[start++] = 6;
                    cc_number[start++] = 0;
                    cc_number[start++] = 1;
                    cc_number[start++] = 1;
                    break;
                case "MasterCard":
                    cc_number[start++] = 5;
                    cc_number[start++] = (int)Math.Floor(r.NextDouble() * 5) + 1;
                    break;
                case "Amex":
                    cc_number[start++] = 3;
                    cc_number[start++] = r.Next(2) == 1 ? 7 : 4;
                    cc_len = 15;
                    break;
            }

            for (int i = start; i < (cc_len - 1); i++)
            {
                cc_number[i] = (int)Math.Floor(r.NextDouble() * 10);
            }

            int sum = 0;
            for (int j = 0; j < (cc_len - 1); j++)
            {
                int digit = cc_number[j];
                if ((j & 1) == (cc_len & 1)) digit *= 2;
                if (digit > 9) digit -= 9;
                sum += digit;
            }

            int[] check_digit = new int[] { 0, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
            cc_number[cc_len - 1] = check_digit[sum % 10];

            string result = string.Empty;
            foreach (int digit in cc_number)
            {
                result += digit;
            }
            return result;
        }
    }
}

